package com.superhelper.superhelper_web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fasterxml.jackson.core.type.TypeReference;
import com.superhelper.common.domain.AuserDeliver;
import com.superhelper.common.domain.DadaReasons;
import com.superhelper.common.domain.TakeOutOrder;
import com.superhelper.common.domain.TakeOutShopInfo;
import com.superhelper.superhelper_web.common.CommonHelper;
import com.superhelper.superhelper_web.common.Consts;
import com.superhelper.superhelper_web.controller.base.AbstractController;
import com.superhelper.superhelper_web.domain.Admin;
import com.superhelper.superhelper_web.domain.DeliverRecordOrder;
import com.superhelper.superhelper_web.domain.PageModel;
import com.superhelper.superhelper_web.domain.Resp;
import com.superhelper.superhelper_web.service.DeliverService;
import com.superhelper.superhelper_web.service.MtService;
import com.superhelper.superhelper_web.service.ShopService;
import com.superhelper.superhelper_web.service.deliver.IDeliverService;

import eleme.openapi.sdk.api.entity.shop.OShop;
import eleme.openapi.sdk.api.exception.ServiceException;

@Controller
@RequestMapping("admin/deliver")
public class DeliverController extends AbstractController {

	@Autowired
	private DeliverService deliverService;
	@Autowired
	private IDeliverService dadaService;

	/**
	 * 设置
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("set")
	public String set(ModelMap model, HttpServletRequest request,
			PageModel pageModel) throws Exception {

		Admin user = CommonHelper.getSessionUser();
		AuserDeliver entity = deliverService.getAuserDeliver(user.getId());
		model.addAttribute("deliver", entity);
		return "admin/deliver/set";
	}

	/**
	 * 设置保存
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("setSave")
	@ResponseBody
	public Resp setSave(ModelMap model, AuserDeliver entity, PageModel pageModel) {
		Admin user = CommonHelper.getSessionUser();
		entity.setAuserId(Long.valueOf(user.getId()));
		int res = deliverService.addSet(entity);
		return res > 0 ? success("保存成功") : fail("保存失败");
	}

	/**
	 * 配送状态修改
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("switch")
	@ResponseBody
	public Resp deliverSwitch(@RequestParam Integer state,
			@RequestParam Integer auserId, HttpServletResponse response)
			throws IOException {
		int res = deliverService.updateDeliverSwitch(auserId, state);
		return res > 0 ? success("") : fail("设置失败");
	}

	/**
	 * 达达订单列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("list")
	public String list(ModelMap model, HttpServletRequest request, int status,
			PageModel pageModel) throws Exception {

		Admin user = CommonHelper.getSessionUser();
		List<DeliverRecordOrder> list = deliverService.getDeliverRecordList(
				user.getId(), status, pageModel);
		model.addAttribute("list", list);
		model.addAttribute("status", status);
		return "admin/deliver/list";
	}

	/**
	 * 达达增加小费
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("tips")
	@ResponseBody
	public Resp tips(Long id, Double tips) throws Exception {
		int res = deliverService.addTips(id, tips);
		return res > 0 ? success("") : fail("");
	}

	/**
	 * 取消订单原因列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("reason")
	public String reasonList(ModelMap model, Long id) {
		String reason_key = "reasonList_key";
		List<DadaReasons> list = redisService.get(reason_key,
				new TypeReference<List<DadaReasons>>() {
				});
		if (list == null || list.size() == 0) {
			list = deliverService.getReasonsList(id);
			redisService.set(reason_key, list);
		}
		model.addAttribute("list", list);
		model.addAttribute("recordId", id);
		return "admin/deliver/reason";
	}

	/**
	 * 取消订单
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("cancel")
	@ResponseBody
	public Resp cancel(Long id, Integer reasonId) throws Exception {
		int res = deliverService.cancel(id, reasonId);
		return res > 0 ? success("") : fail("");
	}

	// /**
	// * 发单页面
	// *
	// * @return
	// * @throws ServiceException
	// */
	// @RequestMapping("deliver")
	// public String deliver(ModelMap model, Long orderId, PageModel pageModel)
	// throws Exception {
	// TakeOutOrder order = dadaService.queryDeliverFee(orderId);
	// if (order != null) {
	// TakeOutShopInfo shop = shopService.getShopInfo(order.getShopId(),
	// order.getType());
	// model.addAttribute("order", order);
	// model.addAttribute("shop", shop);
	// }
	// return "admin/order/deliver";
	// }

	@RequestMapping("test")
	@ResponseBody
	public Resp testData(Long orderId, Integer type) {
		boolean b = dadaService.testOrder(orderId, type);
		return b ? success("") : fail("");
	}

	@RequestMapping("test_e")
	public String test_e(ModelMap model) {
		return "admin/deliver/test";
	}
}
